package com.neusoft.weekreport.component.bizService.impl;

import com.neusoft.weekreport.component.bizService.GroupRefUserBizService;
import com.neusoft.weekreport.component.entities.MsGroupRefUser;
import com.neusoft.weekreport.component.entities.MsGroupRefUserExample;
import com.neusoft.weekreport.component.entities.MsUser;
import com.neusoft.weekreport.component.entities.MsUserExample;
import com.neusoft.weekreport.component.req.GroupRefUserByGroupIdReq;
import com.neusoft.weekreport.component.req.GroupRefUserDeleteUserReq;
import com.neusoft.weekreport.component.resp.GroupRefUserByGroupIdResp;
import com.neusoft.weekreport.mapper.MsGroupRefUserMapper;
import com.neusoft.weekreport.mapper.MsUserMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class GroupRefUserBizServiceImpl implements GroupRefUserBizService {

    @Resource
    MsGroupRefUserMapper msGroupRefUserMapper;
    @Resource
    MsUserMapper msUserMapper;

    //根据groupId查询用户列表
    @Override
    public GroupRefUserByGroupIdResp getUserByGroupId(GroupRefUserByGroupIdReq req) throws Exception {
        GroupRefUserByGroupIdResp resp = new GroupRefUserByGroupIdResp();
        MsGroupRefUserExample msGroupRefUserExample = new MsGroupRefUserExample();
        MsGroupRefUserExample.Criteria criteria = msGroupRefUserExample.createCriteria();
        criteria.andGroupidEqualTo(req.getGroupId());
        List<MsGroupRefUser> msGroupRefUserList = msGroupRefUserMapper.selectByExample(msGroupRefUserExample);
        MsUserExample msUserExample = new MsUserExample();
        MsUserExample.Criteria criteria1 = msUserExample.createCriteria();
        criteria1.andEnableEqualTo(1);
        List<MsUser> msUsers = new ArrayList<>();
        List<String> list = new ArrayList<>();
        if(!CollectionUtils.isEmpty(msGroupRefUserList)){
            for (MsGroupRefUser msGroupRefUser : msGroupRefUserList) {
                list.add(msGroupRefUser.getUserid());
            }
            criteria1.andIdIn(list);
            if(!StringUtils.isEmpty(req.getAccountName())){
                criteria1.andAccountNameLike("%" + req.getAccountName() +"%");
            }
            if(!StringUtils.isEmpty(req.getUserName())){
                criteria1.andUserNameLike("%" + req.getUserName() +"%");
            }
            msUsers = msUserMapper.selectByExample(msUserExample);
        }
        resp.setMsUsers(msUsers);
        return resp;
    }

    //删除关联用户
    @Override
    public void deleteUserByUserId(GroupRefUserDeleteUserReq req) throws Exception {
        MsGroupRefUserExample msGroupRefUserExample = new MsGroupRefUserExample();
        MsGroupRefUserExample.Criteria criteria = msGroupRefUserExample.createCriteria();
        criteria.andGroupidEqualTo(req.getGroupId());
        criteria.andUseridIn(req.getUserIdList());
        msGroupRefUserMapper.deleteByExample(msGroupRefUserExample);

    }

    //查询未关联用户
    @Override
    public GroupRefUserByGroupIdResp getUserNoByGroupId(GroupRefUserByGroupIdReq req) throws Exception {
        GroupRefUserByGroupIdResp resp =new GroupRefUserByGroupIdResp();
        MsGroupRefUserExample msGroupRefUserExample = new MsGroupRefUserExample();
        MsGroupRefUserExample.Criteria criteria = msGroupRefUserExample.createCriteria();
        criteria.andGroupidEqualTo(req.getGroupId());
        List<MsGroupRefUser> msGroupRefUserList = msGroupRefUserMapper.selectByExample(msGroupRefUserExample);

        MsUserExample msUserExample = new MsUserExample();
        MsUserExample.Criteria criteria1 = msUserExample.createCriteria();
        criteria1.andEnableEqualTo(1);
        if(!CollectionUtils.isEmpty(msGroupRefUserList)){
            List<String> list = new ArrayList<>();
            for (MsGroupRefUser msGroupRefUser : msGroupRefUserList) {
                list.add(msGroupRefUser.getUserid());
            }
            criteria1.andIdNotIn(list);
        }
        if(!StringUtils.isEmpty(req.getAccountName())){
            criteria1.andAccountNameEqualTo(req.getAccountName());
        }
        if(!StringUtils.isEmpty(req.getUserName())){
            criteria1.andUserNameLike("%" + req.getUserName() + "%");
        }

        List<MsUser> msUsers = msUserMapper.selectByExample(msUserExample);
        resp.setMsUsers(msUsers);
        return resp;
    }


    @Override
    @Transactional
    public void addUserListByGroupId(GroupRefUserDeleteUserReq req) throws Exception {
        MsGroupRefUser msGroupRefUser = new MsGroupRefUser();
        msGroupRefUser.setGroupid(req.getGroupId());
        List<String> userIdList = req.getUserIdList();
        for (String userId : userIdList) {
            msGroupRefUser.setUserid(userId);
            msGroupRefUserMapper.insert(msGroupRefUser);
        }
    }
}
